//
//  HotelTableViewController.m
//  Travel assistant
//
//  Created by lanou3g on 16/3/1.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "HotelTableViewController.h"
#import "HotelTableVCell.h"
#import "JourneyTVController.h"
#import "HotelDetailVController.h"

@interface HotelTableViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, BMKPoiSearchDelegate>
{
    BMKPoiSearch *_jjPoiSearch;
    NSInteger curPage;
    CLLocationCoordinate2D location;
}
@property (nonatomic, strong) UITextField *textFiled;
@property (nonatomic, strong) HotelDetalModel *hotelDetalModele;
@end

@implementation HotelTableViewController
static NSString *const hotelTableVCell = @"HotelTableVCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationBar];
    [self setDelegate];
    [self setRegisterHotelTableVCell];
    
    
    [self.addHotel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addHotelButtonAction:)]];
    [self setBaiduMapSearchWithKeyword:@"快捷酒店"];
    
}








- (void)setBaiduMapSearchWithKeyword:(NSString *)keyword{
    _jjPoiSearch = [[BMKPoiSearch alloc] init];
    _jjPoiSearch.delegate = self;

    curPage = 0;
    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc] init];
    citySearchOption.pageIndex = (int)curPage;
    //分页数量，可选，默认为10，最多为50
    citySearchOption.pageCapacity = 50;
    citySearchOption.city = @"北京";
    //搜索关键字
    citySearchOption.keyword = keyword;
    /**
     *城市POI检索
     *异步函数，返回结果在BMKPoiSearchDelegate的onGetPoiResult通知
     *@param option 城市内搜索的搜索参数类（BMKCitySearchOption）
     *@return 成功返回YES，否则返回NO
     */
    BOOL flag = [_jjPoiSearch poiSearchInCity:citySearchOption];
    if (flag) {
        NSLog(@"周边检索发送成功......");
    } else {
        NSLog(@"周边检索发送失败......");
    }
}
#pragma mark ---异步函数，返回结果在BMKPoiSearchDelegate的onGetPoiResult通知---
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    self.hotelArray = [NSArray arrayWithArray:result.poiInfoList];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.hotelTableView reloadData];
    });
}




- (void)addHotelButtonAction:(UIView *)sender{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"创建酒店住宿" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        self.textFiled = textField;
        self.textFiled.placeholder = @"请输入酒店名称";
        self.textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textFiled.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.hotelLable.text = self.textFiled.text;
        self.hotelDetalModele = [HotelDetalModel object];
        [self.hotelDetalModele setObject:self.textFiled.text forKey:@"hotelName"];
        [self.hotelDetalModele saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"self.hotelDetalModele存储成功......");
            } else {
                NSLog(@"%@",error);
            }
        }];        
        [self.navigationController popViewControllerAnimated:YES];
    }]];
    [self presentViewController:alertController animated:YES completion:^{
    }];
}




- (void)setNavigationBar{
    self.searchBar = [JJSearchBar jjSearchBar];
    self.searchBar.delegate = self;
    //当点击UITextField的时候不要弹出键盘
    //searchBar.inputView = [[UIView alloc]initWithFrame:CGRectZero];
    //尺寸
    self.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width - 100, 30);
    //设置中间的标题内容
    self.navigationItem.titleView = self.searchBar;
}
#pragma  mark ---UITextField的代理方法---
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self setBaiduMapSearchWithKeyword:textField.text];
    [self.searchBar resignFirstResponder];
    return YES;
}



#pragma mark --- 注册Cell ---
- (void)setRegisterHotelTableVCell{
    [self.hotelTableView registerNib:[UINib nibWithNibName:@"HotelTableVCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:hotelTableVCell];
}




- (void)setDelegate{
    self.hotelTableView.delegate = self;
    self.hotelTableView.dataSource = self;
}
#pragma mark ---与TableView有关的代理方法---
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.hotelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HotelTableVCell *cell = [self.hotelTableView dequeueReusableCellWithIdentifier:hotelTableVCell forIndexPath:indexPath];
    cell.hotelModel = self.hotelArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.searchBar resignFirstResponder];
    HotelDetailVController *hotelDetailVC = [[HotelDetailVController alloc] init];
    hotelDetailVC.passHotelModel = self.hotelArray[indexPath.row];
    [self.navigationController pushViewController:hotelDetailVC animated:YES];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
